package net.dopan.pigframe.api.fegin;

import feign.codec.Encoder;
import net.dopan.pigframe.api.fegin.fallback.OSSServiceFallback;
import net.dopan.pigframe.config.FeignDisableHystrixConfiguration;
import net.dopan.pigframe.config.FeignSpringFormEncoder;
import net.dopan.pigframe.core.base.controller.response.ResponseBean;
import net.dopan.pigframe.core.interceptor.FeignOauth2RequestInterceptor;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import java.util.List;

/***
 * @Package: net.dopan.pigframe.api.fegin
 * @Title: OSSServiceApi
 * @Description: OSS服务接口
 * @author: 刘宽
 * @date: 2018/8/30 17:32
 */
@FeignClient(name = "ossServer", fallback = OSSServiceFallback.class, configuration = {FeignDisableHystrixConfiguration.class, OSSServiceApi.FeignMultipartSupportConfig.class, FeignOauth2RequestInterceptor.class})
public interface OSSServiceApi {

    class FeignMultipartSupportConfig {

        @Bean
        public Encoder encoder(){
            return new FeignSpringFormEncoder();
        }

        @Bean
        public feign.Logger.Level multipartLoggerLevel() {
            return feign.Logger.Level.FULL;
        }

        @Bean
        public CommonsMultipartResolver commonsMultipartResolver() {
            final CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
            commonsMultipartResolver.setMaxUploadSize(50*1024*1024);
            return commonsMultipartResolver;
        }
    }

    /**
     * 上传文件
     *
     MockMultipartFile mockMultipartFile    = null;
     try {
     mockMultipartFile = new MockMultipartFile("file", "1.txt", "txt", new FileInputStream(new File("D://1.txt")));
     } catch (IOException e) {
     e.printStackTrace();
     }
     System.out.println(ossServiceApi.upload(mockMultipartFile).toString());
     * @param file
     * @return
     */
    @PostMapping(value = "/api/upload", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseBean<String> upload(@RequestPart("file") MultipartFile file);

    /**
     * 文件上传(多)
     *
     * @param file
     * @return 是否成功
     */
    @PostMapping(value = "/api/uploads", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    ResponseBean<List<String>> uploads(@RequestPart("file") MultipartFile[] file);

}
